package com.liuf.di.controller;

import com.liuf.di.api.ServiceException;
import com.liuf.di.common.CommonResponse;
import com.liuf.di.common.ResponseUtil;
import com.liuf.di.configuration.kettle.KettleInitFactory;
import com.liuf.di.service.ITranService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.TransMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 转换管理
 * @author: Mr.Liu
 * @create: 2018-09-19 17:34
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Api(value = "Tran-manager",description = "转换管理API")
@RestController
@RequestMapping(value = "/api/tran")
public class TranController {

    private static final Logger logger = LoggerFactory.getLogger(TranController.class);

    /**参数*/
    private Map<String,Object> params = null;

    @Autowired
    private ITranService tranServiceImpl;

    private TransMeta transMeta;

    @ApiOperation(value = "获取转换列表")
    @GetMapping(value = "/find")
    public CommonResponse findTran(@ApiParam(value = "分页参数-页数")@RequestParam(name = "pageNum", defaultValue = "0") int pageNum,
                                   @ApiParam(value = "分页参数-条数")@RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                   @ApiParam(value = "转换名称") @RequestParam(required = false) String name,
                                   @ApiParam(value = "转换详情") @RequestParam(required = false) String description){
        params = new HashMap<>();
        params.put("pageNum",pageNum);
        params.put("pageSize",pageSize);
        params.put("name",name);
        params.put("description",description);
        try {
           return ResponseUtil.generateResponse(tranServiceImpl.findTrans(params));
        } catch (ServiceException e) {
            e.printStackTrace();
            return ResponseUtil.generateResponse(false);
        }
    }

    @ApiOperation(value = "获取单一转换")
    @GetMapping(value = "/get/{id}")
    public CommonResponse getTran(@ApiParam(value = "转换主键")@PathVariable long id){
        try {
            return ResponseUtil.generateResponse(tranServiceImpl.getTran(id));
        } catch (ServiceException e) {
            e.printStackTrace();
            return ResponseUtil.generateResponse(false);
        }
    }

    @ApiOperation(value = "删除转换")
    @DeleteMapping(value = "/delete/{id}")
    public CommonResponse delete(@ApiParam(value = "转换主键")@PathVariable long id){
        return null;
    }


    @ApiOperation(value = "[TRANS]手动执行转换")
    @PostMapping(value = "/execution/{id}/{name}")
    public CommonResponse execution(@ApiParam(value = "转换路径ID")@PathVariable long id,@ApiParam(value = "转换名称")@PathVariable String name){
        transMeta = new TransMeta();
        try {
            transMeta = tranServiceImpl.loadTran(name,id);
            tranServiceImpl.TranExecution(transMeta);
            return ResponseUtil.generateResponse(true);
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("转换执行失败"+e.getMessage());
            return ResponseUtil.generateResponse(false);
        }
    }
}
